//
//  BaseViewController.m
//  MyApp
//
//  Created by sfpay on 14/12/30.
//  Copyright (c) 2014年 Zain. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
@property (nonatomic, strong) NSOperationQueue *backgroudQueue;
@end

@implementation BaseViewController

#pragma mark -
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //add observe for network
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didXZHReceiveNetworkStateChanged:) name:@"kReachabilityChangedNotification" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
 
    //remove observe for network
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kReachabilityChangedNotification" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAllControllerBackgroundImage];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:@"kReachabilityChangedNotification"
     object:nil];
    
    _backgroudQueue = nil;
}

#pragma mark -
- (void)tabBarItemClicked{
    NSLog(@"\ntabBarItemClicked : %@", NSStringFromClass([self class]));
}

#pragma mark - 所有控制器的默认背景
- (void) setAllControllerBackgroundImage {
    // setting default bg
}

#pragma mark -
- (UIRectEdge)edgesForExtendedLayout {
    return UIRectEdgeNone;
}

#pragma mark - 所有控制器的网络状态改变处理

#pragma mark -
- (BOOL)isHaveNet {
    //TODO: 获取当前手机网络是够可到达服务器
    return [ENGINE isReachable];
}

//TODO: 所有控制器的网络状态改变处理
- (void) didXZHReceiveNetworkStateChanged:(NSNotification *) notify {
    
    @autoreleasepool {
        
        //--------------------------------------
        /*
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        [alert showSuccess:self
                     title:MyLocalize(@"NetworkChanged")
                  subTitle:MyLocalize(@"NetworkChangedMessage")
          closeButtonTitle:MyLocalize(@"Done")
                  duration:3];
        */
        
        //--------------------------------------
        /*
        SCLAlertView *alert2 = [[SCLAlertView alloc] init];
        
        //Using Selector
        [alert2 addButton:@"First Button" target:self selector:@selector(firstButtonClick:)];
        
        //Using Block
        [alert2 addButton:@"Second Button" actionBlock:^(void) {
            NSLog(@"Second button tapped");
        }];
        
        //Using Blocks With Validation
        [alert2 addButton:@"Validate" validationBlock:^BOOL {
            //验证字段是否输入合格
            return YES;
            
        } actionBlock:^{
            // handle successful validation here
        }];
        
        //close button
        [alert2 showSuccess:self title:@"Button View" subTitle:@"This alert view has buttons" closeButtonTitle:@"Done" duration:0.0f];
        */
        
        
        //--------------------------------------
        /*
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        alert.attributedFormatBlock = ^NSAttributedString* (NSString *value)
        {
            NSMutableAttributedString *subTitle = [[NSMutableAttributedString alloc]initWithString:value];
            
            NSRange redRange = [value rangeOfString:@"Attributed" options:NSCaseInsensitiveSearch];
            [subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:redRange];
            
            NSRange greenRange = [value rangeOfString:@"successfully" options:NSCaseInsensitiveSearch];
            [subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:greenRange];
            
            NSRange underline = [value rangeOfString:@"completed" options:NSCaseInsensitiveSearch];
            [subTitle addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:underline];
            
            return subTitle;
        };
        [alert showSuccess:self title:@"Button View" subTitle:@"Attributed string operation successfully completed." closeButtonTitle:@"Done" duration:0.0f];
         */
        
        
        //--------------------------------------
        //添加输入框， 键盘打开不会遮盖输入框
        /*
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        
        UITextField *textField = [alert addTextField:@"Enter your name"];
        
        [alert addButton:@"Show Name" actionBlock:^(void) {
            NSLog(@"Text value: %@", textField.text);
        }];
        
        [alert showEdit:self title:@"Edit View" subTitle:@"This alert view shows a text box" closeButtonTitle:@"Done" duration:0.0f];
         */
        
        
        //--------------------------------------
        //点击声音
//        alert.soundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/right_answer.mp3", [[NSBundle mainBundle] resourcePath]]];
        
        
        //--------------------------------------
        //alert dismiss complet
//        [alert alertIsDismissed:^{
//            NSLog(@"SCLAlertView dismissed!");
//        }];
    }
}

#pragma mark - 内存清除
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    //TODO: 清除NSURLCache单例对象的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    //TODO: 清除网络框架使用的缓存
    //1.cacheMemoryDict 2.沙盒缓存目录的data文件
    [ENGINE emptyCache];
}

#pragma mark - add/remove notification
- (void)addNotificationForName:(NSString *)name
                  CompletBlock:(void (^)(NSNotification *notification))block{
    
    [[NSNotificationCenter defaultCenter]
     addObserverForName:name object:self
     queue:_backgroudQueue
     usingBlock:block];
}

- (void)removeNotificationWithNane:(NSString *)name {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:name object:nil];
}

- (void) firstButtonClick:(id) sender {
    
}

@end
